﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using WEB.BLL.Entities;
using WEB.Exceptions;

namespace WEB.BLL
{
    public partial class WEBContext
    {
        #region Constants
        private const string CONST_CUSTOMERSESSION = "WEB.UserSession";
        private const string CONST_CUSTOMERSESSIONCOOKIE = "WEB.UserSessionGUIDCookie";
        #endregion

        #region Fields
        private UserEntity _currentUser;

        private HttpContext _context = HttpContext.Current;
        #endregion

        #region Ctor
        /// <summary>
        /// Creates a new instance of the Context class
        /// </summary>
        private WEBContext()
        {

        }
        #endregion

        #region Methods

        #endregion

        #region Properties
        public object this[string key]
        {
            get
            {
                if (this._context == null)
                {
                    return null;
                }

                if (this._context.Items[key] != null)
                {
                    return this._context.Items[key];
                }
                return null;
            }
            set
            {
                if (this._context != null)
                {
                    this._context.Items.Remove(key);
                    this._context.Items.Add(key, value);

                }
            }
        }
        /// <summary>
        /// Gets an instance of the Context, which can be used to retrieve information about current context.
        /// </summary>
        public static WEBContext Current
        {
            get
            {
                if (HttpContext.Current == null)
                    return null;

                if (HttpContext.Current.Items["WEBContext"] == null)
                {
                    WEBContext context2 = new WEBContext();
                    HttpContext.Current.Items.Add("WEBContext", context2);
                    return context2;
                }
                return (WEBContext)HttpContext.Current.Items["WEBContext"];
            }
        }

        /// <summary>
        /// Gets or sets the current user
        /// </summary>
        public UserEntity User
        {
            get
            {
                return this._currentUser;
            }
            set
            {
                this._currentUser = value;
            }
        }
     




        /// <summary>
        /// Gets an user host address
        /// </summary>
        public string UserHostAddress
        {
            get
            {
                if (HttpContext.Current != null &&
                    HttpContext.Current.Request != null &&
                    HttpContext.Current.Request.UserHostAddress != null)
                    return HttpContext.Current.Request.UserHostAddress;
                else
                    return string.Empty;
            }
        }


        #endregion
    }
}
